package com.sqly.doc.menu.jsjb;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.IdRes;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ihidea.multilinechooselib.MultiLineChooseLayout;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.sqly.doc.R;
import com.sqly.doc.adapter.CommonAdapter;
import com.sqly.doc.adapter.ViewHolder;
import com.sqly.doc.base.MyBaseActivity;
import com.sqly.doc.menu.camera.CameraChoose;
import com.sqly.doc.menu.fjh.fjhsf;
import com.sqly.doc.pojo.JsbYzjshzXxbc;
import com.sqly.doc.pojo.JuMin;
import com.sqly.doc.pojo.RetMsg;
import com.sqly.doc.pojo.Selected;
import com.sqly.doc.pojo.Xx;
import com.sqly.doc.user.choose_user;
import com.sqly.doc.util.BinderPojo;
import com.sqly.doc.util.BinderUtil;
import com.sqly.doc.util.CreateBitmap;
import com.sqly.doc.util.DateConvert;
import com.sqly.doc.util.HttpUrl;
import com.sqly.doc.util.MyToastUtil;
import com.sqly.doc.util.NetWorkUtils;
import com.sqly.doc.util.SharedPreferencesUtils;
import com.sqly.doc.util.SweetAlertDialogUtil;
import com.sqly.doc.util.Url;
import com.sqly.doc.util.UserClient;
import com.sqly.doc.util.WriteActivity;
import com.sqly.doc.view.CustomDatePicker;
import com.sqly.doc.view.StringUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Locale;

//import cn.pedant.SweetAlert.SweetAlertDialog;
import com.cazaea.sweetalert.SweetAlertDialog;

import cz.msebera.android.httpclient.Header;

/**
 * Created by Administrator on 2017/11/22 0022.
 */

public class jsb_xxbc extends MyBaseActivity{

    //标题
    TextView title, uname;
    LinearLayout back;
    Context con = jsb_xxbc.this;
    private CustomDatePicker customDatePicker;

    //只需要在每个ID上设置对应的注解即可
    @BinderPojo(value = R.id.SFJLH, name = "SFJLH", pojoProperty = true)
    private TextView SFJLH;

    @BinderPojo(value = R.id.XINGMING, name = "XINGMING", pojoProperty = true)
    private TextView XINGMING;

    @BinderPojo(value = R.id.SFRQ, name = "SFRQ", pojoProperty = true)
    private TextView SFRQ;

    @BinderPojo(value = R.id.JHRXM, name = "JHRXM", pojoProperty = true)
    private EditText JHRXM;

    @BinderPojo(value = R.id.YHZGX, name = "YHZGX", pojoProperty = true)
    private EditText YHZGX;

    @BinderPojo(value = R.id.JHRZZ, name = "JHRZZ", pojoProperty = true)
    private EditText JHRZZ;

    @BinderPojo(value = R.id.JHRDH, name = "JHRDH", pojoProperty = true)
    private EditText JHRDH;

    @BinderPojo(value = R.id.JWHLXR, name = "JWHLXR", pojoProperty = true)
    private EditText JWHLXR;

    @BinderPojo(value = R.id.JWHLXRDH, name = "JWHLXRDH", pojoProperty = true)
    private EditText JWHLXRDH;

    @BinderPojo(value = R.id.ZQTYQZ, name = "ZQTYQZ", pojoProperty = true)
    private EditText ZQTYQZ;//知情同意签字

    @BinderPojo(value = R.id.JWZYZZQT, name = "JWZYZZQT", pojoProperty = true)
    private EditText JWZYZZQT;//既往主要症状其他

    @BinderPojo(value = R.id.JWZLZYCS, name = "JWZLZYCS", pojoProperty = true)
    private EditText JWZLZYCS;//既往治疗情况-曾住精神专科医院/综合医院精神专科

    @BinderPojo(value = R.id.MQZDQK, name = "MQZDQK", pojoProperty = true)
    private EditText MQZDQK;//目前诊断情况-诊断

    @BinderPojo(value = R.id.QZYY, name = "QZYY", pojoProperty = true)
    private EditText QZYY;//目前诊断情况-确诊医院

    @BinderPojo(value = R.id.QDZS, name = "QDZS", pojoProperty = true)
    private EditText QDZS;//轻度滋事次数

    @BinderPojo(value = R.id.ZHAOSHI, name = "ZHAOSHI", pojoProperty = true)
    private EditText ZHAOSHI;//肇事次数

    @BinderPojo(value = R.id.ZHAOHUO, name = "ZHAOHUO", pojoProperty = true)
    private EditText ZHAOHUO;//肇祸次数

    @BinderPojo(value = R.id.QTWHXW, name = "QTWHXW", pojoProperty = true)
    private EditText QTWHXW;

    @BinderPojo(value = R.id.ZISHANG, name = "ZISHANG", pojoProperty = true)
    private EditText ZISHANG;

    @BinderPojo(value = R.id.ZSWS, name = "ZSWS", pojoProperty = true)
    private EditText ZSWS;

    @BinderPojo(value = R.id.ZKYSYJ, name = "ZKYSYJ", pojoProperty = true)
    private EditText ZKYSYJ;

    @BinderPojo(value = R.id.ZQRQ, name = "ZQRQ", pojoProperty = true)
    private TextView ZQRQ;

    @BinderPojo(value = R.id.CCFBRQ, name = "CCFBRQ", pojoProperty = true)
    private TextView CCFBRQ;

    @BinderPojo(value = R.id.SCKJSBYZLRQ, name = "SCKJSBYZLRQ", pojoProperty = true)
    private TextView SCKJSBYZLRQ;

    @BinderPojo(value = R.id.QZRQ, name = "QZRQ", pojoProperty = true)
    private TextView QZRQ;


    RadioGroup HUBIE,SFZQ,GSQK,JWZLMZ,ZJZLXG,JJQK ;
    Spinner JYQK;
    String jyqks = "1";
    MultiLineChooseLayout jwzyzz;
    private List<String> jwzyzzdata = new ArrayList<>();

    //精神病信息补充
    JsbYzjshzXxbc obj;
    boolean isadd=false;//是否是新添加
    String url="",JKDAH="";
    ImageView sfysqm, brqm, xcqm;
    String sfysqmpic = "", brqmpic = "",xcqmpic ="";
    List<Selected> slist = new ArrayList<>();
    JuMin j;

    ImageView xcpaizhao;
    TextView pz_record_id,jmqm_record_id, ysqm_record_id;
    boolean pzChange = false;

    @Override
    protected void initUI() {
        setContentView(R.layout.jsb_xxbcb);
        BinderUtil.exeFindById(jsb_xxbc.this);
        if (NetWorkUtils.isNetworkConnected(con)) {
            if (getIntent().getStringExtra("type").equals("edit")) {
                obj = (JsbYzjshzXxbc) getIntent().getExtras().getSerializable("b");
                init(obj);
                if(!StringUtils.isEmpty(obj.getImg())){
                    //ImageLoader.getInstance().displayImage(Url.img_url()+obj.getImg(),brqm);
                    ImageLoader.getInstance().displayImage(Url.newStaticurl(SharedPreferencesUtils.getParam(con, "token", "").toString(), "2" ,obj.getImg()),brqm);
                }
                if(!StringUtils.isEmpty(obj.getImg1())){
                    //ImageLoader.getInstance().displayImage(Url.img_url()+obj.getImg1(),sfysqm);
                    ImageLoader.getInstance().displayImage(Url.newStaticurl(SharedPreferencesUtils.getParam(con, "token", "").toString(), "2" ,obj.getImg1()),sfysqm);
                }
                if(!StringUtils.isEmpty(obj.getImg2())){
                    //ImageLoader.getInstance().displayImage(Url.img_url()+obj.getImg2(),xcqm);
                    ImageLoader.getInstance().displayImage(Url.newStaticurl(SharedPreferencesUtils.getParam(con, "token", "").toString(), "2" ,obj.getImg2()),xcqm);
                }
            }else {
                obj = new JsbYzjshzXxbc();
                init(obj);
            }
        }
    }

    private void init(final JsbYzjshzXxbc obj){
        //初始化TextView组件
        initTextView(obj);
        //初始化EditText组件
        initEditText();
        //初始化RadioGroup组件
        initRadioGroup();
        //初始化Spinner组件
        initSpinner();
        //初始化MultiLineChooseLayout组件
        initMultiLineChooseLayout();
    }
    private void initTextView(final JsbYzjshzXxbc obj){
        //运行时生成activity中所有控件的findViewById方法
        //BinderUtil.exeFindById(fjhdycrhsf.this);
        BinderUtil.initData(jsb_xxbc.this, obj);
        //医生签名
        sfysqm = (ImageView) findViewById(R.id.sfysqm);
        if (obj != null && !StringUtils.isEmpty(obj.getSfysqm())) {
            sfysqmpic = obj.getSfysqm();
            sfysqm.setImageBitmap(BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/" + sfysqmpic + ".jpg"));
        }
        sfysqm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(con, WriteActivity.class)
                        .putExtra("cardnum", obj.getCARDNUM())
                        .putExtra("tabId", null==obj.getSFJLH()||"".equals(obj.getSFJLH()) ? "noid" : obj.getSFJLH())
                        .putExtra("tabType", "zxjsbxxbc")
                        .putExtra("imageType", "img1")
                        .putExtra("class", "jsb_xxbc.class"), 8);
            }
        });
        //本人签名
        brqm = (ImageView) findViewById(R.id.brqm);
        if (obj != null && !StringUtils.isEmpty(obj.getBrqm())) {
            brqmpic = obj.getBrqm();
            brqm.setImageBitmap(BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/" + brqmpic + ".jpg"));
        }
        brqm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(con, WriteActivity.class)
                        .putExtra("cardnum", obj.getCARDNUM())
                        .putExtra("tabId", null==obj.getSFJLH()||"".equals(obj.getSFJLH()) ? "noid" : obj.getSFJLH())
                        .putExtra("tabType", "zxjsbxxbc")
                        .putExtra("imageType", "img")
                        .putExtra("class", "jsb_xxbc.class"), 9);
            }
        });
        xcqm = (ImageView) findViewById(R.id.xcqm);
        if (obj != null && !StringUtils.isEmpty(obj.getXcqm())) {
            xcqmpic = obj.getXcqm();
            xcqm.setImageBitmap(BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/" + xcqmpic + ".jpg"));
        }
        xcqm.setOnClickListener(new CustomerListener());
        //现场拍照
        xcpaizhao = (ImageView) findViewById(R.id.xcpaizhao);
        xcpaizhao.setOnClickListener(new CustomerListener());
        //现场拍照照片id
        pz_record_id = (TextView) findViewById(R.id.pz_record_id);
        if(obj != null && !StringUtils.isEmpty(obj.getImg2())) {
            //fjhdycrhsf/D24E75C5BB5E417EBE65494CA09BF399.jpg
            String[] img2Arr =obj.getImg2().split("/");
            if(null !=img2Arr && img2Arr.length>1){
                pz_record_id.setText(img2Arr[1]);
            }
        }
        jmqm_record_id= (TextView) findViewById(R.id.jmqm_record_id);
        if(obj != null && !StringUtils.isEmpty(obj.getImg())) {
            String[] img2Arr =obj.getImg().split("/");
            if(null !=img2Arr && img2Arr.length>1){
                jmqm_record_id.setText(img2Arr[1]);
            }
        }
        ysqm_record_id= (TextView) findViewById(R.id.ysqm_record_id);
        if(obj != null && !StringUtils.isEmpty(obj.getImg1())) {
            //fjhdycrhsf/D24E75C5BB5E417EBE65494CA09BF399.jpg
            String[] img2Arr =obj.getImg1().split("/");
            if(null !=img2Arr && img2Arr.length>1){
                ysqm_record_id.setText(img2Arr[1]);
            }
        }

        //填表日期
        SFRQ = (TextView) findViewById(R.id.SFRQ);
        if(obj != null && !StringUtils.isEmpty(obj.getSFRQ())){
            SFRQ.setText(DateConvert.dateToString(DateConvert.toDate(obj.getSFRQ(), DateConvert.YMD), DateConvert.YMD)+"");
        }
        SFRQ.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
                String now = sdf.format(new Date());
                customDatePicker = new CustomDatePicker(con, new CustomDatePicker.ResultHandler() {
                    @Override
                    public void handle(String time) { // 回调接口，获得选中的时间
                        SFRQ.setText(time.split(" ")[0]);
                    }
                }, "2010-01-01 00:00", now); // 初始化日期格式请用：yyyy-MM-dd HH:mm，否则不能正常运行
                customDatePicker.showSpecificTime(false); // 不显示时和分
                customDatePicker.setIsLoop(false); // 不允许循环滚动
                customDatePicker.show(now);
            }
        });
        //姓名
        XINGMING = (TextView) findViewById(R.id.XINGMING);
        XINGMING.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!NetWorkUtils.isNetworkConnected(con)) {
                    MyToastUtil.ShowToast(con, "联网失败！");
                }else {
                    Bundle bundle = getIntent().getExtras();
                    if (null != bundle.getString("type") && "add".equals(bundle.getString("type"))) {
                        startActivityForResult(new Intent(con, choose_user.class).putExtra("cname", "jsb_xxbc.class"), 1);
                    }
                }

            }
        });
        back = (LinearLayout) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(pzChange){
                   SweetAlertDialogUtil.WarningDialogHaveCancelAndConfirm(con,"你确认不保存数据更新吗？","现场拍照已经采集，请注意点击“保存”","确定", "返回");
                }else {
                    finish();
                }
            }
        });
        title = (TextView) findViewById(R.id.title);
        uname = (TextView) findViewById(R.id.uname);
        title.setText("严重精神障碍患者个人信息补充");
        uname.setText("保存");
        uname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean validate = BinderUtil.copyDataToView(jsb_xxbc.this, obj);
                if(!validate){

                }else if(null == XINGMING.getText().toString() || "".equals(XINGMING.getText().toString())){
                    SweetAlertDialogUtil.WarningDialog(con, "提示", "姓名不能为空");
                }else if(null == SFRQ.getText().toString() || "".equals(SFRQ.getText().toString())){
                    SweetAlertDialogUtil.WarningDialog(con, "提示", "填表日期不能为空");
                }else if ("1".equals(SharedPreferencesUtils.getParam(con, "ysqm", "0").toString()) && ysqm_record_id.getText().toString().equals("noid")) {
                    SweetAlertDialogUtil.WarningDialog(con, "提示", "随访医生签名不能为空");
                }else if ("1".equals(SharedPreferencesUtils.getParam(con, "jmqm", "0").toString()) && jmqm_record_id.getText().toString().equals("noid")) {
                    SweetAlertDialogUtil.WarningDialog(con, "提示", "家属或本人签名不能为空");
                }else if ("1".equals(SharedPreferencesUtils.getParam(con, "xcpz", "0").toString()) && pz_record_id.getText().toString().equals("noid")) {
                    SweetAlertDialogUtil.WarningDialog(con, "提示", "现场拍照不能为空");

                } else if (NetWorkUtils.isNetworkConnected(con)) {  ///网络连接
                    RequestParams ps = BinderUtil.generateRequestParamsByView(jsb_xxbc.this);
                    if (isadd) {
                        ps.add("entity.JKDAH", JKDAH);
                        url = HttpUrl.AddJsbXxbc;
                    } else {
                        ps.add("entity.SFJLH", obj.getSFJLH());
                        ps.add("entity.JKDAH", obj.getJKDAH());
                        url = HttpUrl.EditJsbXxbc;
                    }
                    ps.add("entity.HUBIE", obj.getHUBIE());
                    ps.add("entity.SFZQ",obj.getSFZQ());
                    ps.add("entity.GSQK", obj.getGSQK());
                    ps.add("entity.JWZLMZ", obj.getGSQK());
                    ps.add("entity.ZJZLXG", obj.getGSQK());
                    ps.add("entity.JJQK", obj.getJJQK());
                    ps.add("entity.JYQK", obj.getJYQK());
                    //既往主要症状
                    ArrayList<Integer> jwslist = jwzyzz.getAllItemSelectedIndex();
                    String jwss = "";
                    for (int i = 0; i < jwslist.size(); i++) {
                        if (i == 0)
                            jwss = (Integer.parseInt(jwslist.get(i)+"")+1) + "";
                        else
                            jwss = jwss + "," + (Integer.parseInt(jwslist.get(i)+"")+1);
                    }
                    ps.add("entity.JWZYZZ", jwss);
                    ps.add("entity.CARDNUM", obj.getCARDNUM());
                    ps.add("token", SharedPreferencesUtils.getParam(con, "token", "").toString());
                    ps.add("type", "2");

                    ps.add("pz_record_id", pz_record_id.getText().toString());
                    ps.add("jmqm_record_id", jmqm_record_id.getText().toString());
                    ps.add("ysqm_record_id", jmqm_record_id.getText().toString());
                    ps.add("entity.UPDATETIME", obj.getUPDATETIME());
                    //jsb_xxbc.super.ImageUploadOperate(sfysqmpic, brqmpic, ps);
                    UserClient.get(url, ps, new AsyncHttpResponseHandler() {
                        @Override
                        public void onSuccess(int statusCode, Header[] headers, byte[] response) {
                            String content = new String(response);
                            RetMsg ret = JSON.parseObject(content, RetMsg.class);
                            if (ret.getCode().equals("200")) {
                                MyToastUtil.ShowToast(con, "成功");
                            }else{
                                MyToastUtil.ShowToast(con, ret.getInfo());
                            }
                            finish();
                        }

                        @Override
                        public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {

                            MyToastUtil.ShowToast(con, "失败");
                        }
                    });
                } else {

                }

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null) {

        } else if (requestCode == 1) {
            j = (JuMin) data.getExtras().getSerializable("j");
            getObject();
        }else if (requestCode == 8) {
            String record_id = (String) data.getExtras().getString("record_id");
            ysqm_record_id.setText(record_id);
            byte[] bis = data.getByteArrayExtra("bitmap");
            Bitmap bmp = BitmapFactory.decodeByteArray(bis, 0, bis.length);
            Bitmap bb = CreateBitmap.zoomImg(bmp, 1, 1);
            sfysqm.setImageBitmap(bb);
            sfysqmpic = CreateBitmap.saveMyBitmap(bb);
        }else if (requestCode == 9) {
            String record_id = (String) data.getExtras().getString("record_id");
            jmqm_record_id.setText(record_id);
            byte[] bis = data.getByteArrayExtra("bitmap");
            Bitmap bmp = BitmapFactory.decodeByteArray(bis, 0, bis.length);
            Bitmap bb = CreateBitmap.zoomImg(bmp, 1, 1);
            brqm.setImageBitmap(bb);
            brqmpic = CreateBitmap.saveMyBitmap(bb);
        }else if(requestCode == 119){
            String img_record_id = (String) data.getExtras().getString("img_record_id");
            pz_record_id.setText(img_record_id);
            pzChange = true;
            byte[] bis = data.getByteArrayExtra("bitmap");
            Bitmap bmp = BitmapFactory.decodeByteArray(bis, 0, bis.length);
            Bitmap bb = CreateBitmap.zoomImg(bmp, 1, 1);
            xcqm.setImageBitmap(bb);
        }
    }

    public void getObject() {
        RequestParams ps = new RequestParams();
        ps.add("token", SharedPreferencesUtils.getParam(con, "token", "").toString());
        ps.add("type", "2");
        ps.add("CARDNUM", j.getCARDNUM());
        UserClient.get(HttpUrl.GetJsbXxbcGrjbxx, ps, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] response) {
                String content = new String(response);
                RetMsg ret = JSON.parseObject(content, RetMsg.class);
                if (ret.getCode().equals("200")) {
                    JSONObject j = JSON.parseObject(ret.getRd());
                    obj = new JsbYzjshzXxbc();
                    init(obj);
                    obj.setJKDAH(j.getString("JKDAH"));
                    obj.setXINGMING(j.getString("XINGMING"));
                    obj.setCARDNUM(j.getString("CARDNUM"));
                    JKDAH = j.getString("JKDAH");
                    XINGMING.setText(j.getString("XINGMING"));

                    isadd = true;
                } else {
                    SweetAlertDialogUtil.ErrorDialogNoFinish(con, "错误提示", ret.getInfo(), "确定");
                }

            }
            @Override
            public void onFinish() {
                super.onFinish();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {

                isadd = true;
                obj = new JsbYzjshzXxbc();
                init(obj);
            }
        });
    }

    /****
     * 初始化EditText组件
     */
    private void initEditText(){
        JHRXM=(EditText)findViewById(R.id.JHRXM);
        if (obj != null && !StringUtils.isEmpty(obj.getJHRXM())) {
            JHRXM.setText(obj.getJHRXM());
        }
        YHZGX=(EditText)findViewById(R.id.YHZGX);
        if (obj != null && !StringUtils.isEmpty(obj.getYHZGX())) {
            YHZGX.setText(obj.getYHZGX());
        }

        JHRZZ =(EditText)findViewById(R.id.JHRZZ);
        if (obj != null && !StringUtils.isEmpty(obj.getJHRZZ())) {
            JHRZZ.setText(obj.getJHRZZ());
        }

        JHRDH = (EditText)findViewById(R.id.JHRDH);
        if (obj != null && !StringUtils.isEmpty(obj.getJHRDH())) {
            JHRDH.setText(obj.getJHRDH());
        }
        //辖区村（居）委会联系人、电话
        JWHLXR = (EditText)findViewById(R.id.JWHLXR);
        if (obj != null && !StringUtils.isEmpty(obj.getJWHLXR())) {
            JWHLXR.setText(obj.getJWHLXR());
        }
        JWHLXRDH = (EditText)findViewById(R.id.JWHLXRDH);
        if (obj != null && !StringUtils.isEmpty(obj.getJWHLXRDH())) {
            JWHLXRDH.setText(obj.getJWHLXRDH());
        }
        //知情通知签字
        ZQTYQZ = (EditText)findViewById(R.id.ZQTYQZ);
        if (obj != null && !StringUtils.isEmpty(obj.getZQTYQZ())) {
            ZQTYQZ.setText(obj.getZQTYQZ());
        }
        ZQRQ = (TextView)findViewById(R.id.ZQRQ);
        if(obj != null && !StringUtils.isEmpty(obj.getZQRQ())){
            ZQRQ.setText(DateConvert.dateToString(DateConvert.toDate(obj.getZQRQ(), DateConvert.YMD), DateConvert.YMD)+"    ");
        }
        ZQRQ.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
                String now = sdf.format(new Date());
                customDatePicker = new CustomDatePicker(con, new CustomDatePicker.ResultHandler() {
                    @Override
                    public void handle(String time) { // 回调接口，获得选中的时间
                        ZQRQ.setText(time.split(" ")[0]);
                    }
                }, "2010-01-01 00:00", now); // 初始化日期格式请用：yyyy-MM-dd HH:mm，否则不能正常运行
                customDatePicker.showSpecificTime(false); // 不显示时和分
                customDatePicker.setIsLoop(false); // 不允许循环滚动
                customDatePicker.show(now);
            }
        });
        CCFBRQ = (TextView) findViewById(R.id.CCFBRQ);
        if(obj != null && !StringUtils.isEmpty(obj.getCCFBRQ())){
            CCFBRQ.setText(DateConvert.dateToString(DateConvert.toDate(obj.getCCFBRQ(), DateConvert.YMD), DateConvert.YMD)+"    ");
        }
        CCFBRQ.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
                String now = sdf.format(new Date());
                customDatePicker = new CustomDatePicker(con, new CustomDatePicker.ResultHandler() {
                    @Override
                    public void handle(String time) { // 回调接口，获得选中的时间
                        CCFBRQ.setText(time.split(" ")[0]);
                    }
                }, "2010-01-01 00:00", now); // 初始化日期格式请用：yyyy-MM-dd HH:mm，否则不能正常运行
                customDatePicker.showSpecificTime(false); // 不显示时和分
                customDatePicker.setIsLoop(false); // 不允许循环滚动
                customDatePicker.show(now);
            }
        });
        //其他既往症状
        JWZYZZQT = (EditText)findViewById(R.id.JWZYZZQT);
        if (obj != null && !StringUtils.isEmpty(obj.getJWZYZZQT())) {
            JWZYZZQT.setText(obj.getJWZYZZQT());
        }
        SCKJSBYZLRQ = (TextView)findViewById(R.id.SCKJSBYZLRQ);
        if(obj != null && !StringUtils.isEmpty(obj.getSCKJSBYZLRQ())){
            SCKJSBYZLRQ.setText(DateConvert.dateToString(DateConvert.toDate(obj.getSCKJSBYZLRQ(), DateConvert.YMD), DateConvert.YMD)+"    ");
        }
        SCKJSBYZLRQ.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
                String now = sdf.format(new Date());
                customDatePicker = new CustomDatePicker(con, new CustomDatePicker.ResultHandler() {
                    @Override
                    public void handle(String time) { // 回调接口，获得选中的时间
                        SCKJSBYZLRQ.setText(time.split(" ")[0]);
                    }
                }, "2010-01-01 00:00", now); // 初始化日期格式请用：yyyy-MM-dd HH:mm，否则不能正常运行
                customDatePicker.showSpecificTime(false); // 不显示时和分
                customDatePicker.setIsLoop(false); // 不允许循环滚动
                customDatePicker.show(now);
            }
        });
        //曾住精神病专科医院/综合医院精神专科次数
        JWZLZYCS = (EditText)findViewById(R.id.JWZLZYCS);
        if (obj != null && !StringUtils.isEmpty(obj.getJWZLZYCS())) {
            JWZLZYCS.setText(obj.getJWZLZYCS());
        }
        //诊断
        MQZDQK=(EditText)findViewById(R.id.MQZDQK);
        if (obj != null && !StringUtils.isEmpty(obj.getMQZDQK())) {
            MQZDQK.setText(obj.getMQZDQK());
        }
        //确诊医院
        QZYY=(EditText)findViewById(R.id.QZYY);
        if (obj != null && !StringUtils.isEmpty(obj.getQZYY())) {
            QZYY.setText(obj.getQZYY());
        }
        //确诊日期
        QZRQ = (TextView) findViewById(R.id.QZRQ);
        if(obj != null && !StringUtils.isEmpty(obj.getQZRQ())){
            QZRQ.setText(DateConvert.dateToString(DateConvert.toDate(obj.getQZRQ(), DateConvert.YMD), DateConvert.YMD)+"    ");
        }
        QZRQ.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
                String now = sdf.format(new Date());
                customDatePicker = new CustomDatePicker(con, new CustomDatePicker.ResultHandler() {
                    @Override
                    public void handle(String time) { // 回调接口，获得选中的时间
                        QZRQ.setText(time.split(" ")[0]);
                    }
                }, "2010-01-01 00:00", now); // 初始化日期格式请用：yyyy-MM-dd HH:mm，否则不能正常运行
                customDatePicker.showSpecificTime(false); // 不显示时和分
                customDatePicker.setIsLoop(false); // 不允许循环滚动
                customDatePicker.show(now);
            }
        });
        //轻度滋事(次)
        QDZS = (EditText)findViewById(R.id.QDZS);
        if (obj != null && !StringUtils.isEmpty(obj.getQDZS())) {
            QDZS.setText(obj.getQDZS());
        }
        //肇事(次)
        ZHAOSHI = (EditText)findViewById(R.id.ZHAOSHI);
        if (obj != null && !StringUtils.isEmpty(obj.getZHAOSHI())) {
            ZHAOSHI.setText(obj.getZHAOSHI());
        }
        //肇祸
        ZHAOHUO = (EditText)findViewById(R.id.ZHAOHUO);
        if (obj != null && !StringUtils.isEmpty(obj.getZHAOHUO())) {
            ZHAOHUO.setText(obj.getZHAOHUO());
        }
        //其他危害行为
        QTWHXW = (EditText)findViewById(R.id.QTWHXW);
        if (obj != null && !StringUtils.isEmpty(obj.getQTWHXW())) {
            QTWHXW.setText(obj.getQTWHXW());
        }
        //自伤
        ZISHANG = (EditText)findViewById(R.id.ZISHANG);
        if (obj != null && !StringUtils.isEmpty(obj.getZISHANG())) {
            ZISHANG.setText(obj.getZISHANG());
        }
        //自杀未遂
        ZSWS= (EditText)findViewById(R.id.ZSWS);
        if (obj != null && !StringUtils.isEmpty(obj.getZSWS())) {
            ZSWS.setText(obj.getZSWS());
        }
        //专科医生的意见 (如果有请记录)
        ZKYSYJ = (EditText)findViewById(R.id.ZKYSYJ);
        if (obj != null && !StringUtils.isEmpty(obj.getZKYSYJ())) {
            ZKYSYJ.setText(obj.getZKYSYJ());
        }
    }
    /***
     * 初始化RadioGroup组件
     */
    private void initRadioGroup(){
        for (int i=1; i<=6; i++){
            Selected s = new Selected();
            s.setName("0");
            s.setPosition(11111);
            slist.add(s);
        }
        //户别
        HUBIE = findViewById(R.id.HUBIE);
        if (obj != null && !StringUtils.isEmpty(obj.getHUBIE())) {
            switch (Integer.parseInt(obj.getHUBIE())) {
                case 1:
                    HUBIE.check(R.id.hb_cz);
                    break;
                case 2:
                    HUBIE.check(R.id.hd_nc);
                    break;
            }
        }
        HUBIE.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.hb_cz) {
                    obj.setHUBIE("1");
                } else {
                    obj.setHUBIE("2");
                }
            }
        });
        //知情同意
        SFZQ = findViewById(R.id.SFZQ);
        if (obj != null && !StringUtils.isEmpty(obj.getSFZQ())) {
            switch (Integer.parseInt(obj.getSFZQ())) {
                case 1:
                    SFZQ.check(R.id.zqty1);
                    break;
                case 0:
                    SFZQ.check(R.id.zqty2);
                    break;
            }
        }
        SFZQ.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.zqty1) {
                    obj.setSFZQ("1");
                } else {
                    obj.setSFZQ("0");
                }
            }
        });
        //既往关锁情况
        GSQK = (RadioGroup) findViewById(R.id.GSQK);
        if (obj != null && !StringUtils.isEmpty(obj.getGSQK())) {
            switch (Integer.parseInt(obj.getGSQK())) {
                case 1:
                    GSQK.check(R.id.jwgsqk1);
                    break;
                case 2:
                    GSQK.check(R.id.jwgsqk2);
                    break;
                case 3:
                    GSQK.check(R.id.jwgsqk3);
                    break;
            }
        }
        GSQK.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if (checkedId == R.id.jwgsqk1)
                    obj.setGSQK("1");
                if (checkedId == R.id.jwgsqk2)
                    obj.setGSQK("2");
                if (checkedId == R.id.jwgsqk3)
                    obj.setGSQK("3");
            }
        });
        //既往治疗情况--门诊
        JWZLMZ = (RadioGroup)findViewById(R.id.JWZLMZ);
        if (obj != null && !StringUtils.isEmpty(obj.getJWZLMZ())) {
            switch (Integer.parseInt(obj.getJWZLMZ())) {
                case 1:
                    JWZLMZ.check(R.id.mz1);
                    break;
                case 2:
                    JWZLMZ.check(R.id.mz2);
                    break;
                case 3:
                    JWZLMZ.check(R.id.mz3);
                    break;
            }
        }
        JWZLMZ.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int checkedId) {
                if (checkedId == R.id.mz1)
                    obj.setJWZLMZ("1");
                if (checkedId == R.id.mz2)
                    obj.setJWZLMZ("2");
                if (checkedId == R.id.mz3)
                    obj.setJWZLMZ("3");
            }
        });
        //最后一次治疗结果
        ZJZLXG = (RadioGroup)findViewById(R.id.ZJZLXG);
        if (obj != null && !StringUtils.isEmpty(obj.getZJZLXG())) {
            switch (Integer.parseInt(obj.getZJZLXG())) {
                case 1:
                    ZJZLXG.check(R.id.zjyczlxg1);
                    break;
                case 2:
                    ZJZLXG.check(R.id.zjyczlxg2);
                    break;
                case 3:
                    ZJZLXG.check(R.id.zjyczlxg3);
                    break;
                case 4:
                    ZJZLXG.check(R.id.zjyczlxg4);
                    break;
            }
        }
        ZJZLXG.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int checkedId) {
                if (checkedId == R.id.zjyczlxg1)
                    obj.setZJZLXG("1");
                if (checkedId == R.id.zjyczlxg2)
                    obj.setZJZLXG("2");
                if (checkedId == R.id.zjyczlxg3)
                    obj.setZJZLXG("3");
                if (checkedId == R.id.zjyczlxg4)
                    obj.setZJZLXG("4");
            }
        });
        //经济状况
        JJQK = (RadioGroup)findViewById(R.id.JJQK);
        if (obj != null && !StringUtils.isEmpty(obj.getJJQK())) {
            switch (Integer.parseInt(obj.getJJQK())) {
                case 1:
                    JJQK.check(R.id.pk);
                    break;
                case 2:
                    JJQK.check(R.id.fpk);
                    break;
            }
        }
        JJQK.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int checkedId) {
                if (checkedId == R.id.pk)
                    obj.setJJQK("1");
                if (checkedId == R.id.fpk)
                    obj.setJJQK("2");
            }
        });
    }
    /***
     * 初始化Spinner组件
     */
    private void initSpinner(){
        JYQK = (Spinner) findViewById(R.id.JYQK);
        Resources res = getResources();
        String[] jydic = res.getStringArray(R.array.jsb_xxbc_jyqk);
        List<Xx> jylist = new ArrayList<Xx>();
        for (int i = 0; i < jydic.length; i++) {
            Xx x = new Xx();
            x.setName(jydic[i]);
            jylist.add(x);
        }
        JYQK.setAdapter(new CommonAdapter<Xx>(jsb_xxbc.this, jylist, R.layout.sp_item) {
            @Override
            public void convert(ViewHolder helper, Xx item) {
                helper.setText(R.id.name, item.getName());
            }
        });
        JYQK.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                jyqks = (position + 1) + "";
                if(obj!=null)
                obj.setJYQK(jyqks);
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        if (obj != null && !StringUtils.isEmpty(obj.getJYQK())) {
            JYQK.setSelection(Integer.parseInt(obj.getJYQK())-1);
        }
    }
    /***
     * 初始化initMultiLineChooseLayout组件
     */
    private void initMultiLineChooseLayout(){
        //既往主要症状
        jwzyzz = (MultiLineChooseLayout) findViewById(R.id.jwzyzz);
        jwzyzzdata.add("幻觉");
        jwzyzzdata.add("交流困难");
        jwzyzzdata.add("猜疑");
        jwzyzzdata.add("喜怒无常");
        jwzyzzdata.add("行为怪异");
        jwzyzzdata.add("兴奋话多");
        jwzyzzdata.add("伤人毁物");
        jwzyzzdata.add("悲观厌世");
        jwzyzzdata.add("无故外走");
        jwzyzzdata.add("自语自笑");
        jwzyzzdata.add("孤僻懒散");
        jwzyzzdata.add("其他");
        jwzyzz.setList(jwzyzzdata);
        if (obj != null && !StringUtils.isEmpty(obj.getJWZYZZ())) {
            String[] zzList = obj.getJWZYZZ().split(",");
            if (Arrays.asList(zzList).contains("1"))
                jwzyzz.setIndexItemSelected(0);
            if (Arrays.asList(zzList).contains("2"))
                jwzyzz.setIndexItemSelected(1);
            if (Arrays.asList(zzList).contains("3"))
                jwzyzz.setIndexItemSelected(2);
            if (Arrays.asList(zzList).contains("4"))
                jwzyzz.setIndexItemSelected(3);
            if (Arrays.asList(zzList).contains("5"))
                jwzyzz.setIndexItemSelected(4);
            if (Arrays.asList(zzList).contains("6"))
                jwzyzz.setIndexItemSelected(5);
            if (Arrays.asList(zzList).contains("7"))
                jwzyzz.setIndexItemSelected(6);
            if (Arrays.asList(zzList).contains("8"))
                jwzyzz.setIndexItemSelected(7);
            if (Arrays.asList(zzList).contains("9"))
                jwzyzz.setIndexItemSelected(8);
            if (Arrays.asList(zzList).contains("10"))
                jwzyzz.setIndexItemSelected(9);
            if (Arrays.asList(zzList).contains("11"))
                jwzyzz.setIndexItemSelected(10);
            if (Arrays.asList(zzList).contains("12"))
                jwzyzz.setIndexItemSelected(11);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }
    class CustomerListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            if(obj != null && StringUtils.isEmpty(obj.getJKDAH())) {
                SweetAlertDialogUtil.WarningDialog(con, "提示", "选择居民用户后才能现场拍照");
            }else {
                Intent intent = new Intent(con, CameraChoose.class)
                        .putExtra("type", "edit")
                        .putExtra("cardnum", obj.getCARDNUM())
                        .putExtra("tabId", null==obj.getSFJLH()||"".equals(obj.getSFJLH()) ? "noid" : obj.getSFJLH())
                        .putExtra("tabType", "zxjsbxxbc")
                        .putExtra("class", "jsb_xxbc.class");
                startActivityForResult(intent, 119);
            }
        }
    }

}
